<?php
class Model_user extends CI_Model {
	
	var $table = "users";
	
    function Model_User()
    {
        // Call the Model constructor
        parent::__construct();
    }
	
	function login($user,$pass){
		$this->load->database();
						
		$sql = "SELECT * FROM {$this->table} WHERE email='$user';";
		$query = $this->db->query($sql);
		
		$this->db->close();
		
		foreach($query->result() as $row){
			if(md5(sha1($pass)) == $row->password && $row->is_active == 'yes'){
				$data = array(
					'login'		=> TRUE,
					'uid'	=> $row->id,
					'name'		=> $row->nama,
					'username'	=> $row->username,
					'pp'	=> $row->profile_image,
					'group'		=> $row->group_id,
				);
				$this->session->set_userdata($data);
				
				$sql = "UPDATE {$this->table} SET last_login='".date('Y-m-h H:i:s')."' WHERE id={$row->id}";
				$this->db->query($sql);
				
				return true;
			}
		}		
		return false;
	}
	
	function forgotpass($email){
		$this->load->database();
		
		$sql = "SELECT * FROM {$this->table} WHERE email='$email';";
		$query = $this->db->query($sql);
		
		$this->db->close();
			
		return $query;
	}
	
	function check($data){
		$this->load->database();						
			$sql = "SELECT {$this->table}.* FROM {$this->table} WHERE username='$data[username]'";
			$query = $this->db->query($sql);

			if($query->num_rows() > 0){
				return "Username yang Anda masukan tidak tersedia";
			}
			
			$sql = "SELECT {$this->table}.* FROM {$this->table} WHERE email='$data[email]'";
			$query = $this->db->query($sql);

			if($query->num_rows() > 0){
				return "Email yang Anda masukan tidak tersedia";
			}
			
		$this->db->close();
		
		return true;
	}
	
	function confirm($token){
		$this->load->database();						
			$sql = "SELECT {$this->table}.* FROM {$this->table} WHERE confirm='$token'";
			$query = $this->db->query($sql);		
		$this->db->close();
		
		return $query;
	}
	
	function get_by($id){
		$this->load->database();
						
			$sql = "SELECT {$this->table}.*, kota.nama kota FROM {$this->table} LEFT JOIN kota ON kota.id=kota_id WHERE users.id='$id';";
			$query = $this->db->query($sql);
		
		$this->db->close();
		
		return $query;
	}
	
	function get_all($page=0,$limit=20, $q=''){
		$this->load->database();
		$filter='';
		$page = $page*$limit;
		if($q != ''){
			$filter = " WHERE (";
			$fields = $this->db->list_fields("{$this->table}");
			foreach ($fields as $field){
			   $filter .= " $field LIKE '%$q%' OR" ;
			}
			$filter = rtrim($filter,'OR').")";
		}

		$sql = "SELECT {$this->table}.*, groups.name `group` FROM {$this->table} LEFT JOIN groups ON group_id=groups.id $filter ORDER BY id ASC LIMIT $page,$limit;";
		$query['data'] = $this->db->query($sql);
		
		$sql = "SELECT {$this->table}.*, groups.name `group` FROM {$this->table} LEFT JOIN groups ON group_id=groups.id $filter";
		$query['count'] = $this->db->query($sql);
		$this->db->close();
		
		return $query;
	}
    
	function add($user){
		$this->load->database();		
		$this->db->trans_start();
			$this->db->insert("{$this->table}",$user);
		$this->db->trans_complete();
		
		$return = $this->db->trans_status();
		
		$this->db->close();
		return $return;
	}

	function edit($id,$data){
		$this->load->database();
			$this->db->where('id',$id);
			$this->db->update("{$this->table}",$data);
		$this->db->close();
	}
	
	function delete($id){
		$this->load->database();
		
		$this->db->trans_start();
			$this->db->query("DELETE FROM {$this->table} WHERE id='$id'");
		$this->db->trans_complete();
		
		$return = $this->db->trans_status();
		
		$this->db->close();
		return $return;
	}
}
?>